/*
 * Copyright (C) 2016 Bilibili
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.yq.skinlib.widgets;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.TextView;

import androidx.annotation.ColorRes;

import com.yq.skinlib.R;
import com.yq.skinlib.manage.SkinCompatResources;
import com.yq.skinlib.utils.TintInfo;


/**
 * @author xyczero617@gmail.com
 * @time 15/9/26
 */
class AppCompatTextHelper extends AppCompatBaseHelper<TextView> {

    //If writing like this:
    //int[] ATTRS = { R.attr.tintText, android.R.attr.textColor, android.R.attr.textColorLink, ...};
    //we can't get textColor value when api is below 20;

    private int mTextColorId;

    private TintInfo mTextColorTintInfo;

    AppCompatTextHelper(TextView view) {
        super(view);
    }

    @SuppressWarnings("ResourceType")
    @Override
    void loadFromAttribute(AttributeSet attrs, int defStyleAttr) {
        final Context context = mView.getContext();

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TintTextHelper, defStyleAttr, 0);

        try {
            int textColorId = a.getResourceId(R.styleable.TintTextHelper_android_textColor, 0);
            if (textColorId == 0) {
                // First read the TextAppearance style id
                final int ap = a.getResourceId(R.styleable.TintTextHelper_android_textAppearance, 0);
                a.recycle();
                if (ap != 0) {
                    a = context.obtainStyledAttributes(ap, R.styleable.TintTextAppearance);
                    if (a.hasValue(R.styleable.TintTextAppearance_android_textColor)) {
                        textColorId = a.getResourceId(R.styleable.TintTextAppearance_android_textColor, 0);
                    }
                    a.recycle();
                }

                // Now read the style's values
                a = context.obtainStyledAttributes(attrs, R.styleable.TintTextAppearance, defStyleAttr, 0);
                if (a.hasValue(R.styleable.TintTextAppearance_android_textColor)) {
                    textColorId = a.getResourceId(R.styleable.TintTextAppearance_android_textColor, 0);
                }
                a.recycle();
            }

            if (textColorId != 0) {
                setTextColor(textColorId);
            }
        } catch (Exception e) {
            e.getMessage();
        } finally {

        }
    }


    /**
     * External use
     */
    public void setTextColor() {
        if (skipNextApply())
            return;

        resetTextColorTintResource(0);
        setSkipNextApply(false);
    }

    public void setTextAppearanceForTextColor(int resId) {
        resetTextColorTintResource(0);
        setTextAppearanceForTextColor(resId, true);
    }

    public void setTextAppearanceForTextColor(int resId, boolean isForced) {
        boolean isTextColorForced = isForced || mTextColorId == 0;
        TypedArray appearance = mView.getContext().obtainStyledAttributes(resId, R.styleable.TintTextAppearance);
        if (appearance.hasValue(R.styleable.TintTextAppearance_android_textColor) && isTextColorForced) {
            setTextColor(appearance.getResourceId(R.styleable.TintTextAppearance_android_textColor, 0));
        }
        appearance.recycle();
    }

    public void setTextColorById(@ColorRes int colorId) {
        setTextColor(colorId);
    }

    /**
     * Internal use
     */
    private void setTextColor(ColorStateList tint) {
        if (skipNextApply())
            return;

        mView.setTextColor(tint);
    }

    private void setTextColor(@ColorRes int resId) {
        if (mTextColorId != resId) {
            resetTextColorTintResource(resId);

            if (resId != 0) {
                setSupportTextColorTint(resId);
            }
        }
    }

    private void setSupportTextColorTint(int resId) {
        if (resId != 0) {
            if (mTextColorTintInfo == null) {
                mTextColorTintInfo = new TintInfo();
            }
            mTextColorTintInfo.mHasTintList = true;
            mTextColorTintInfo.mTintList = SkinCompatResources.getColorStateList(mView.getContext(), resId);
        }
        applySupportTextColorTint();
    }

    private void applySupportTextColorTint() {
        if (mTextColorTintInfo != null && mTextColorTintInfo.mHasTintList) {
            setTextColor(mTextColorTintInfo.mTintList);
        }
    }

    private void resetTextColorTintResource(@ColorRes int resId/*text resource id*/) {
        mTextColorId = resId;
        if (mTextColorTintInfo != null) {
            mTextColorTintInfo.mHasTintList = false;
            mTextColorTintInfo.mTintList = null;
        }
    }

    @Override
    public void tint() {
        if (mTextColorId != 0) {
            setSupportTextColorTint(mTextColorId);
        }
    }

    public interface TextExtensible {
        void setTextColorById(@ColorRes int colorId);
    }
}
